package view;

import service.UserService;
import javax.swing.*;
import java.awt.*;

public class RegisterFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JTextField nameField;
    private JComboBox<String> genderComboBox;
    private JTextField phoneField;
    private JTextField emailField;
    private UserService userService;
    
    public RegisterFrame() {
        userService = new UserService();
        initComponents();
    }
    
    private void initComponents() {
        setTitle("用户注册");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 500);
        setLocationRelativeTo(null);
        
        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        
        // 用户名
        gbc.gridx = 0; gbc.gridy = 0;
        mainPanel.add(new JLabel("用户名："), gbc);
        gbc.gridx = 1;
        usernameField = new JTextField(20);
        mainPanel.add(usernameField, gbc);
        
        // 密码
        gbc.gridx = 0; gbc.gridy = 1;
        mainPanel.add(new JLabel("密码："), gbc);
        gbc.gridx = 1;
        passwordField = new JPasswordField(20);
        mainPanel.add(passwordField, gbc);
        
        // 确认密码
        gbc.gridx = 0; gbc.gridy = 2;
        mainPanel.add(new JLabel("确认密码："), gbc);
        gbc.gridx = 1;
        confirmPasswordField = new JPasswordField(20);
        mainPanel.add(confirmPasswordField, gbc);
        
        // 姓名
        gbc.gridx = 0; gbc.gridy = 3;
        mainPanel.add(new JLabel("姓名："), gbc);
        gbc.gridx = 1;
        nameField = new JTextField(20);
        mainPanel.add(nameField, gbc);
        
        // 性别
        gbc.gridx = 0; gbc.gridy = 4;
        mainPanel.add(new JLabel("性别："), gbc);
        gbc.gridx = 1;
        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        mainPanel.add(genderComboBox, gbc);
        
        // 电话
        gbc.gridx = 0; gbc.gridy = 5;
        mainPanel.add(new JLabel("电话："), gbc);
        gbc.gridx = 1;
        phoneField = new JTextField(20);
        mainPanel.add(phoneField, gbc);
        
        // 邮箱
        gbc.gridx = 0; gbc.gridy = 6;
        mainPanel.add(new JLabel("邮箱："), gbc);
        gbc.gridx = 1;
        emailField = new JTextField(20);
        mainPanel.add(emailField, gbc);
        
        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton registerButton = new JButton("注册");
        JButton cancelButton = new JButton("取消");
        buttonPanel.add(registerButton);
        buttonPanel.add(cancelButton);
        
        // 注册按钮事件
        registerButton.addActionListener(e -> handleRegister());
        
        // 取消按钮事件
        cancelButton.addActionListener(e -> dispose());
        
        // 布局
        setLayout(new BorderLayout());
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private void handleRegister() {
        // 获取输入
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String name = nameField.getText().trim();
        String gender = (String) genderComboBox.getSelectedItem();
        String phone = phoneField.getText().trim();
        String email = emailField.getText().trim();
        
        // 验证输入
        if (username.isEmpty() || password.isEmpty() || name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名、密码和姓名不能为空！");
            return;
        }
        
        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致！");
            return;
        }
        
        // 调用注册服务
        if (userService.register(username, password, "student", name, gender, phone, email)) {
            JOptionPane.showMessageDialog(this, "注册成功！");
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "注册失败，请检查用户名是否已存在！", 
                "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
} 